package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.constants.Constants;
import com.htc.model.dto.MerchantDTO;

public class MerchantModel {
	public static MerchantModel instance;

	public static MerchantModel getInstance() {
		if (instance == null)
			instance = new MerchantModel();
		return instance;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT merchant_id FROM " + Constants.TBL_MERCHANT
				+ " where merchant_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertMerchan(MerchantDTO mer, Context context) {
		boolean flag = false;
		if (!checkDB(context, mer.getMerchantId())) {
			String DATABASE_TABLE = Constants.TBL_MERCHANT;
			ContentValues values = new ContentValues();
			values.put("merchant_id", mer.getMerchantId());
			values.put("merchant_name", mer.getMerchantName());
			values.put("merchant_image", mer.getMerchantImage());
			values.put("priority", mer.getPriority());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateMerchan(MerchantDTO mer, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_MERCHANT;
		String where = "merchant_id='" + mer.getMerchantId() + "'";
		ContentValues values = new ContentValues();

		values.put("merchant_name", mer.getMerchantName());
		values.put("merchant_image", mer.getMerchantImage());
		values.put("priority", mer.getPriority());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteMerchan(MerchantDTO mer, Context context) {
		String sql = "merchant_id='" + mer.getMerchantId() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_MERCHANT;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
